package com.applet.system.interceptor;

import java.util.Date;


import com.applet.system.utils.DateUtils;
import com.applet.system.utils.StringUtils;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;


@Component
public class DateConverterConfig implements Converter<String, Date> {

	@Override
	public Date convert(String source) {
		if (StringUtils.isEmpty(source)) {
			return null;
		}
		String value = source.trim();
		if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")) {
			return DateUtils.parse(value, DateUtils.yyyyMMdd);
		} else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {
			return DateUtils.parse(value, DateUtils.yyyyMMddHHmmss);
		}
		return null;
	}

}
